package de.baumann.browser.fragments

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.widget.RelativeLayout
import android.widget.Toast
import de.baumann.browser.BuildConfig
import de.baumann.browser.R
import de.baumann.browser.activitys.BindPoolActivity
import de.baumann.browser.activitys.NoticeActivity
import de.baumann.browser.activitys.SettingActivity
import de.baumann.browser.activitys.center.ProPlanActivity
import de.baumann.browser.activitys.hash.BuyHashActivity
import de.baumann.browser.activitys.node.NodeListActivity
import de.baumann.browser.base.BaseOdinFragment
import de.baumann.browser.iview.IMineView
import de.baumann.browser.present.MinePresenter
import de.baumann.browser.utils.Constants.LOGINCODE
import de.baumann.browser.utils.getFormAddress
import de.baumann.browser.utils.getInviteCodeStatus
import de.baumann.browser.utils.getIsLogin
import de.baumann.browser.utils.getMoneyAddress
import de.baumann.browser.utils.web3j.BalancePowerTask
import de.baumann.browser.utils.web3j.BalanceTask
import de.baumann.browser.utils.web3j.WalletDaoUtils
import de.baumann.browser.views.dialog.InviteCodeDialog
import kotlinx.android.synthetic.main.fragments_mine.*
import kotlinx.android.synthetic.main.include_tool_bar.*

/**
 * @author:
 * @date:2018/12/6 15:07
 * @discription: 个人主页
 */
class MineFragments : BaseOdinFragment<IMineView, MinePresenter>(), View.OnClickListener, IMineView {

    companion object {
        val instance: MineFragments by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            MineFragments()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)
        bindMoneyAddress = getMoneyAddress()
    }

    override fun getLayout(): Int {
        return R.layout.fragments_mine
    }

    private var bindMoneyAddress: String? = ""
    override fun onResume() {
        super.onResume()
        bindMoneyAddress = getMoneyAddress()
        inviteCodeStatus = getInviteCodeStatus()
        if (bindMoneyAddress != "") {
            tvProfit.text = "0"
            tvPowerValue.text = "0"
            poolAddress.text = "-"
            BalanceTask(tvProfit).execute(getMoneyAddress())
            getMoneyAddress()?.let { BalancePowerTask(tvPowerValue, BuildConfig.WALLET_NODE_URL, BuildConfig.BUY_HASH_ADDRESS, it).execute() }
            if (!getIsLogin()) {
                getPresenter().login()
            }
        } else {
            showLoginDialog()
        }
        poolAddress.text = getFormAddress()
        var wallet = getMoneyAddress()
        if (wallet?.startsWith("0x") == true) {
            wallet = "od" + wallet?.substring(1)
        }
        mineAddress.text = wallet
        tvWalletName.text = WalletDaoUtils.getWalletName(bindMoneyAddress)

    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)

    }

    private var inviteCodeStatus: Int = 0
    override fun initView(view: View) {
        inviteCodeStatus = getInviteCodeStatus()
        bindMoneyAddress = getMoneyAddress()
        (activity as AppCompatActivity).setSupportActionBar(commToolBar)
        val layoutParams = commToolBar.layoutParams as RelativeLayout.LayoutParams
        layoutParams.setMargins(0, 0, 0, 0)
        tvBindPool.setOnClickListener(this)
        tvMinePool.setOnClickListener(this)
        tvBuyHash.setOnClickListener(this)
        var wallet = getMoneyAddress()
        if (wallet?.startsWith("0x") == true) {
            wallet = "od" + wallet?.substring(1)
        }
        mineAddress.text = wallet
        tvWalletName.text = WalletDaoUtils.getWalletName(bindMoneyAddress)
        poolAddress.text = getFormAddress()
    }


    override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
        inflater!!.inflate(R.menu.menu_mine, menu)
        super.onCreateOptionsMenu(menu, inflater)

    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item!!.itemId) {
            R.id.menu_notice -> {
                if (bindMoneyAddress != "") {
                    startActivity(Intent(mContext, NoticeActivity::class.java))
                } else {
                    showLoginDialog()
                }
            }
            R.id.menu_setting -> {
                SettingActivity.startSettingActivity(mContext)
            }
        }

        return super.onOptionsItemSelected(item)
    }

    override fun initData() {
    }

    override fun onNewIntent() {
//        if (!isLogined()) {
//            setTotalProfit("0.0000")
//            setTotalHash("0.0000")
//            showLoginDialog()
//        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && requestCode == LOGINCODE) {
            initData()
        }
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.tvMinePool -> {
                if (bindMoneyAddress == "") {
                    showLoginDialog()
                    return
                } else {
                    ProPlanActivity.startProPlanActivity(mContext)
                }
            }

            R.id.tvBuyHash -> {
                if (bindMoneyAddress == "") {
                    showLoginDialog()
                    return
                } else {
                    if (inviteCodeStatus > 0) {
                        BuyHashActivity.startBuyHashActivity(mContext)
                    } else {
                        InviteCodeDialog(mContext, View.OnClickListener {
                            BindPoolActivity.startBindPoolActivity(mContext)
                        }).show()
                    }

                }

            }
            R.id.tvBindPool -> {
                if (bindMoneyAddress == "") {
                    showLoginDialog()
                    return
                } else {
                    if (inviteCodeStatus > 0) {
                        Toast.makeText(mContext, "已绑定矿池", Toast.LENGTH_SHORT).show()
                        return
                    } else {
                        BindPoolActivity.startBindPoolActivity(mContext)
                    }
                }

            }
        }
    }


    override fun setLoginName(loginName: String?) {
    }

    override fun setHeadhUrl(headUrl: String?) {

    }

    override fun setPoolName(poolName: String?) {
    }

    override fun setLicenceStr(licenceStr: String) {
    }

    override fun setTotalProfit(profit: String) {
        tvProfit!!.text = profit
    }

    override fun setTotalHash(hash: String) {
        tvPowerValue!!.text = hash
    }

    override fun startNodeList() {
        NodeListActivity.startNodeListActivity(mContext)
    }

    override fun startNodeStatus(nodeId: String) {
    }

    override fun createPresenter(): MinePresenter? {
        return MinePresenter()
    }

    override fun createView(): IMineView? {
        return this
    }
}
